package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.AbandonStudent;
import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.FormalStudent;
import cn.wolfcode.crm.domain.TransferClass;
import cn.wolfcode.crm.mapper.DictionaryItemMapper;
import cn.wolfcode.crm.mapper.FormalStudentMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.*;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class FormalStudentServiceImpl implements IFormalStudentService {
    @Autowired
    private FormalStudentMapper formalStudentMapper;
    @Autowired
    private ITransferClassService transferClassService;
    @Autowired
    private IClassRoomService classRoomService;
    @Autowired
    private IAbandonStudentService abandonStudentService;
    @Autowired
    private IClientService clientService;
    @Autowired
    private DictionaryItemMapper dictionaryItemMapper;

    public int insert(FormalStudent record) {
        return formalStudentMapper.insert(record);
    }

    public int updateByPrimaryKey(FormalStudent record) {
        Client client = new Client();
        FormalStudent formalStudent = formalStudentMapper.selectByPrimaryKey(record.getId());
        client.setId(formalStudent.getClientId());
        client.setName(record.getName());
        client.setAge(record.getAge());
        client.setGender(record.getGender());
        client.setAddress(record.getAddress());
        client.setSalemen(record.getSalemen());
        client.setTel(record.getTel());
        client.setQq(record.getQq());
        client.setMajor(record.getMajor());
        client.setSource(record.getSource());
        client.setIntentionClass(record.getIntentionClass());
        client.setIntentionArea(dictionaryItemMapper.selectByPrimaryKey(record.getIntentionArea()));
        client.setSchoolName(record.getSchoolName());
        client.setEmail(record.getEmail());
        client.setEducation(record.getEducation());
        clientService.updateClient(client);
        return formalStudentMapper.updateByPrimaryKey(record);
    }


    public List<FormalStudent> selectAll() {
        return formalStudentMapper.selectAll();
    }

    public FormalStudent selectById(Long id) {
        return formalStudentMapper.selectById(id);
    }

    public PageResult query(QueryObject qo) {
        int total = formalStudentMapper.query4Count(qo);
        if (total > 0) {
            return new PageResult(total, formalStudentMapper.query4List(qo));
        }
        return new PageResult(total, Collections.EMPTY_LIST);
    }

    public int deleteByPrimaryKey(Long id) {

        return formalStudentMapper.deleteByPrimaryKey(id);
    }

    public void transfer(Long targetClassId, Long id) {
        FormalStudent formalStudent = formalStudentMapper.selectByPrimaryKey(id);
        TransferClass transferClass = new TransferClass();
        transferClass.setCustomerType(formalStudent.getCustomerType().getId());
        transferClass.setDebt(formalStudent.getDebt());
        transferClass.setName(formalStudent.getName());
        transferClass.setPrepaidTuition(formalStudent.getPrepaidTuition());
        transferClass.setStatus(false);
        transferClass.setPreviousClass(formalStudent.getIntentionClass());
        transferClass.setWantClass(classRoomService.selectByPrimaryKey(targetClassId));
        transferClass.setQq(formalStudent.getQq());
        transferClass.setSalemen(formalStudent.getSalemen());
        transferClass.setTel(formalStudent.getTel());
        transferClass.setTotalTuition(formalStudent.getTotalTuition());
        transferClass.setTransferTime(new Date());
        transferClassService.insert(transferClass);
        formalStudentMapper.transferStatus(id);
    }

    public void bandon(Long formalStudentId, AbandonStudent abandonStudent) {
        FormalStudent formalStudent = formalStudentMapper.selectByPrimaryKey(formalStudentId);
        abandonStudent.setFormalStudent(formalStudent);
        formalStudentMapper.updateAbandonStatus(formalStudentId);
        abandonStudent.setAbandonTime(new Date());
        Date entranceTime = formalStudent.getEntranceTime();
        abandonStudent.setStatus(false);
        //计算上课时间
        long time1 = new Date().getTime();
        long time2 = entranceTime.getTime();
        long numberDay = (time1 - time2);
        numberDay = numberDay / (1000 * 60 * 60 * 24);
        abandonStudent.setNumberDay(numberDay);
        abandonStudent.setOperator(UserContext.getCurrentUser());
        abandonStudentService.insert(abandonStudent);
    }

    public List<FormalStudent> selectAllPayed() {
        return formalStudentMapper.selectAllPayed();
    }

    public void audit(Long id, BigDecimal amount) {

        FormalStudent formalStudent = formalStudentMapper.selectByPrimaryKey(id);
        BigDecimal debt = formalStudent.getDebt();
        BigDecimal prepaidTuition = formalStudent.getPrepaidTuition();
        prepaidTuition = prepaidTuition.add(amount);

        if (debt.subtract(amount).compareTo(BigDecimal.ZERO) <= 0) {
            debt = BigDecimal.ZERO;
            formalStudentMapper.auditDebtClear(id, prepaidTuition, debt);
            return;
        }
        debt = debt.subtract(amount);
        formalStudentMapper.auditDebtNotClear(id, prepaidTuition, debt);
    }
}
